home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / FloatingDecimal.java < prev    next >
Text File  |  1998-09-22  |  39KB  |  1,368 lines

  1. /*
  2.  * @(#)FloatingDecimal.java    1.10 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. class FloatingDecimal{
  18.     boolean    isExceptional;
  19.     boolean    isNegative;
  20.     int        decExponent;
  21.     char    digits[];
  22.     int        nDigits;
  23.  
  24.     private    FloatingDecimal( boolean negSign, int decExponent, char []digits, int n,  boolean e )
  25.     {
  26.     isNegative = negSign;
  27.     isExceptional = e;
  28.     this.decExponent = decExponent;
  29.     this.digits = digits;
  30.     this.nDigits = n;
  31.     }
  32.  
  33.     /*
  34.      * Constants of the implementation
  35.      * Most are IEEE-754 related.
  36.      * (There are more really boring constants at the end.)
  37.      */
  38.     static final long    signMask = 0x8000000000000000L;
  39.     static final long    expMask  = 0x7ff0000000000000L;
  40.     static final long    fractMask= ~(signMask|expMask);
  41.     static final int    expShift = 52;
  42.     static final int    expBias  = 1023;
  43.     static final long    fractHOB = ( 1L<<expShift ); // assumed High-Order bit
  44.     static final long    expOne     = ((long)expBias)<<expShift; // exponent of 1.0
  45.     static final int    maxSmallBinExp = 62;
  46.     static final int    minSmallBinExp = -( 63 / 3 );
  47.  
  48.     static final long    highbyte = 0xff00000000000000L;
  49.     static final long    highbit  = 0x8000000000000000L;
  50.     static final long    lowbytes = ~highbyte;
  51.  
  52.     static final int    singleSignMask =    0x80000000;
  53.     static final int    singleExpMask  =    0x7f800000;
  54.     static final int    singleFractMask =   ~(singleSignMask|singleExpMask);
  55.     static final int    singleExpShift    =   23;
  56.     static final int    singleFractHOB    =   1<<singleExpShift;
  57.     static final int    singleExpBias    =   127;
  58.  
  59.     /*
  60.      * count number of bits from high-order 1 bit to low-order 1 bit,
  61.      * inclusive.
  62.      */
  63.     private static int
  64.     countBits( long v ){
  65.     // 
  66.     // the strategy is to shift until we get a non-zero sign bit
  67.     // then shift until we have no bits left, counting the difference.
  68.     // we do byte shifting as a hack. Hope it helps.
  69.     //
  70.     if ( v == 0L ) return 0;
  71.  
  72.     while ( ( v & highbyte ) == 0L ){
  73.         v <<= 8;
  74.     }
  75.     while ( v > 0L ) { // i.e. while ((v&highbit) == 0L )
  76.         v <<= 1;
  77.     }
  78.  
  79.     int n = 0;
  80.     while (( v & lowbytes ) != 0L ){
  81.         v <<= 8;
  82.         n += 8;
  83.     }
  84.     while ( v != 0L ){
  85.         v <<= 1;
  86.         n += 1;
  87.     }
  88.     return n;
  89.     }
  90.  
  91.     /*
  92.      * Keep big powers of 5 handy for future reference.
  93.      */
  94.     private static FDBigInt b5p[];
  95.  
  96.     private static FDBigInt
  97.     big5pow( int p ){
  98.     if ( p < 0 )
  99.         throw new RuntimeException( "Assertion botch: negative power of 5");
  100.     if ( b5p == null ){
  101.         b5p = new FDBigInt[ p+1 ];
  102.     }else if (b5p.length <= p ){
  103.         FDBigInt t[] = new FDBigInt[ p+1 ];
  104.         System.arraycopy( b5p, 0, t, 0, b5p.length );
  105.         b5p = t;
  106.     }
  107.     if ( b5p[p] != null )
  108.         return b5p[p];
  109.     else if ( p < small5pow.length )
  110.         return b5p[p] = new FDBigInt( small5pow[p] );
  111.     else if ( p < long5pow.length )
  112.         return b5p[p] = new FDBigInt( long5pow[p] );
  113.     else {
  114.         // construct the damn thing.
  115.         // recursively.
  116.         int q, r;
  117.         // in order to compute 5^p,
  118.         // compute its square root, 5^(p/2) and square.
  119.         // or, let q = p / 2, r = p -q, then
  120.         // 5^p = 5^(q+r) = 5^q * 5^r
  121.         q = p >> 1;
  122.         r = p - q;
  123.         FDBigInt bigq =  b5p[q];
  124.         if ( bigq == null ) 
  125.         bigq = big5pow ( q );
  126.         if ( r < small5pow.length ){
  127.         return (b5p[p] = bigq.mult( small5pow[r] ) );
  128.         }else{
  129.         FDBigInt bigr = b5p[ r ];
  130.         if ( bigr == null ) 
  131.             bigr = big5pow( r );
  132.         return (b5p[p] = bigq.mult( bigr ) );
  133.         }
  134.     }
  135.     }
  136.  
  137.     /*
  138.      * This is the easy subcase -- 
  139.      * all the significant bits, after scaling, are held in lvalue.
  140.      * negSign and decExponent tell us what processing and scaling
  141.      * has already been done. Exceptional cases have already been
  142.      * stripped out. 
  143.      * In particular:
  144.      * lvalue is a finite number (not Inf, nor NaN)
  145.      * lvalue > 0L (not zero, nor negative).
  146.      *
  147.      * The only reason that we develop the digits here, rather than
  148.      * calling on Long.toString() is that we can do it a little faster,
  149.      * and besides want to treat trailing 0s specially. If Long.toString
  150.      * changes, we should re-evaluate this strategy!
  151.      */
  152.     private void
  153.     developLongDigits( int decExponent, long lvalue, long insignificant ){
  154.     char digits[];
  155.     int  ndigits;
  156.     int  digitno;
  157.     int  c;
  158.     //
  159.     // Discard non-significant low-order bits, while rounding,
  160.     // up to insignificant value.
  161.     int i;
  162.     for ( i = 0; insignificant >= 10L; i++ )
  163.         insignificant /= 10L;
  164.     if ( i != 0 ){
  165.         long pow10 = long5pow[i] << i; // 10^i == 5^i * 2^i;
  166.         long residue = lvalue % pow10;
  167.         lvalue /= pow10;
  168.         decExponent += i;
  169.         if ( residue >= (pow10>>1) ){
  170.         // round up based on the low-order bits we're discarding
  171.         lvalue++;
  172.         }
  173.     }
  174.     if ( lvalue <= Integer.MAX_VALUE ){
  175.         if ( lvalue <= 0L )
  176.         throw new RuntimeException("Assertion botch: value "+lvalue+" <= 0");
  177.  
  178.         // even easier subcase!
  179.         // can do int arithmetic rather than long!
  180.         int  ivalue = (int)lvalue;
  181.         digits = new char[ ndigits=10 ];
  182.         digitno = ndigits-1;
  183.         c = ivalue%10;
  184.         ivalue /= 10;
  185.         while ( c == 0 ){
  186.         decExponent++;
  187.         c = ivalue%10;
  188.         ivalue /= 10;
  189.         }
  190.         while ( ivalue != 0){
  191.         digits[digitno--] = (char)(c+'0');
  192.         decExponent++;
  193.         c = ivalue%10;
  194.         ivalue /= 10;
  195.         }
  196.         digits[digitno] = (char)(c+'0');
  197.     } else {
  198.         // same algorithm as above (same bugs, too )
  199.         // but using long arithmetic.
  200.         digits = new char[ ndigits=20 ];
  201.         digitno = ndigits-1;
  202.         c = (int)(lvalue%10L);
  203.         lvalue /= 10L;
  204.         while ( c == 0 ){
  205.         decExponent++;
  206.         c = (int)(lvalue%10L);
  207.         lvalue /= 10L;
  208.         }
  209.         while ( lvalue != 0L ){
  210.         digits[digitno--] = (char)(c+'0');
  211.         decExponent++;
  212.         c = (int)(lvalue%10L);
  213.         lvalue /= 10;
  214.         }
  215.         digits[digitno] = (char)(c+'0');
  216.     }
  217.     char result [];
  218.     ndigits -= digitno;
  219.     if ( digitno == 0 )
  220.         result = digits;
  221.     else {
  222.         result = new char[ ndigits ];
  223.         System.arraycopy( digits, digitno, result, 0, ndigits );
  224.     }
  225.     this.digits = result;
  226.     this.decExponent = decExponent+1;
  227.     this.nDigits = ndigits;
  228.     }
  229.  
  230.     //
  231.     // add one to the least significant digit.
  232.     // in the unlikely event there is a carry out,
  233.     // deal with it.
  234.     // assert that this will only happen where there
  235.     // is only one digit, e.g. (float)1e-44 seems to do it.
  236.     //
  237.     private void
  238.     roundup(){
  239.     int i;
  240.     int q = digits[ i = (nDigits-1)];
  241.     if ( q == '9' ){
  242.         while ( q == '9' && i > 0 ){
  243.         digits[i] = '0';
  244.         q = digits[--i];
  245.         }
  246.         if ( q == '9' ){
  247.         // carryout! High-order 1, rest 0s, larger exp.
  248.         decExponent += 1;
  249.         digits[0] = '1';
  250.         return;
  251.         }
  252.         // else fall through.
  253.     }
  254.     digits[i] = (char)(q+1);
  255.     }
  256.  
  257.     /*
  258.      * FIRST IMPORTANT CONSTRUCTOR: DOUBLE
  259.      */
  260.     public FloatingDecimal( double d )
  261.     {
  262.     long    dBits = Double.doubleToLongBits( d );
  263.     long    fractBits;
  264.     int    binExp;
  265.     int    nSignificantBits;
  266.  
  267.     // discover and delete sign
  268.     if ( (dBits&signMask) != 0 ){
  269.         isNegative = true;
  270.         dBits ^= signMask;
  271.     } else {
  272.         isNegative = false;
  273.     }
  274.     // Begin to unpack
  275.     // Discover obvious special cases of NaN and Infinity.
  276.     binExp = (int)( (dBits&expMask) >> expShift );
  277.     fractBits = dBits&fractMask;
  278.     if ( binExp == (int)(expMask>>expShift) ) {
  279.         isExceptional = true;
  280.         if ( fractBits == 0L ){
  281.         digits =  infinity;
  282.         } else {
  283.         digits = notANumber;
  284.         isNegative = false; // NaN has no sign!
  285.         }
  286.         nDigits = digits.length;
  287.         return;
  288.     }
  289.     isExceptional = false;
  290.     // Finish unpacking
  291.     // Normalize denormalized numbers.
  292.     // Insert assumed high-order bit for normalized numbers.
  293.     // Subtract exponent bias.
  294.     if ( binExp == 0 ){
  295.         if ( fractBits == 0L ){
  296.         // not a denorm, just a 0!
  297.         decExponent = 0;
  298.         digits = zero;
  299.         nDigits = 1;
  300.         return;
  301.         }
  302.         while ( (fractBits&fractHOB) == 0L ){
  303.         fractBits <<= 1;
  304.         binExp -= 1;
  305.         }
  306.         nSignificantBits = expShift + binExp; // recall binExp is  - shift count.
  307.         binExp += 1;
  308.     } else {
  309.         fractBits |= fractHOB;
  310.         nSignificantBits = expShift+1;
  311.     }
  312.     binExp -= expBias;
  313.     // call the routine that actually does all the hard work.
  314.     dtoa( binExp, fractBits, nSignificantBits );
  315.     }
  316.  
  317.     /*
  318.      * SECOND IMPORTANT CONSTRUCTOR: SINGLE
  319.      */
  320.     public FloatingDecimal( float f )
  321.     {
  322.     int    fBits = Float.floatToIntBits( f );
  323.     int    fractBits;
  324.     int    binExp;
  325.     int    nSignificantBits;
  326.  
  327.     // discover and delete sign
  328.     if ( (fBits&singleSignMask) != 0 ){
  329.         isNegative = true;
  330.         fBits ^= singleSignMask;
  331.     } else {
  332.         isNegative = false;
  333.     }
  334.     // Begin to unpack
  335.     // Discover obvious special cases of NaN and Infinity.
  336.     binExp = (int)( (fBits&singleExpMask) >> singleExpShift );
  337.     fractBits = fBits&singleFractMask;
  338.     if ( binExp == (int)(singleExpMask>>singleExpShift) ) {
  339.         isExceptional = true;
  340.         if ( fractBits == 0L ){
  341.         digits =  infinity;
  342.         } else {
  343.         digits = notANumber;
  344.         isNegative = false; // NaN has no sign!
  345.         }
  346.         nDigits = digits.length;
  347.         return;
  348.     }
  349.     isExceptional = false;
  350.     // Finish unpacking
  351.     // Normalize denormalized numbers.
  352.     // Insert assumed high-order bit for normalized numbers.
  353.     // Subtract exponent bias.
  354.     if ( binExp == 0 ){
  355.         if ( fractBits == 0 ){
  356.         // not a denorm, just a 0!
  357.         decExponent = 0;
  358.         digits = zero;
  359.         nDigits = 1;
  360.         return;
  361.         }
  362.         while ( (fractBits&singleFractHOB) == 0 ){
  363.         fractBits <<= 1;
  364.         binExp -= 1;
  365.         }
  366.         nSignificantBits = singleExpShift + binExp; // recall binExp is  - shift count.
  367.         binExp += 1;
  368.     } else {
  369.         fractBits |= singleFractHOB;
  370.         nSignificantBits = singleExpShift+1;
  371.     }
  372.     binExp -= singleExpBias;
  373.     // call the routine that actually does all the hard work.
  374.     dtoa( binExp, ((long)fractBits)<<(expShift-singleExpShift), nSignificantBits );
  375.     }
  376.  
  377.     private void
  378.     dtoa( int binExp, long fractBits, int nSignificantBits )
  379.     {
  380.     int    nFractBits; // number of significant bits of fractBits;
  381.     int    nTinyBits;  // number of these to the right of the point.
  382.     int    decExp;
  383.  
  384.     // Examine number. Determine if it is an easy case,
  385.     // which we can do pretty trivially using float/long conversion,
  386.     // or whether we must do real work.
  387.     nFractBits = countBits( fractBits );
  388.     nTinyBits = Math.max( 0, nFractBits - binExp - 1 );
  389.     if ( binExp <= maxSmallBinExp && binExp >= minSmallBinExp ){
  390.         // Look more closely at the number to decide if,
  391.         // with scaling by 10^nTinyBits, the result will fit in
  392.         // a long.
  393.         if ( (nTinyBits < long5pow.length) && ((nFractBits + n5bits[nTinyBits]) < 64 ) ){
  394.         /*
  395.          * We can do this:
  396.          * take the fraction bits, which are normalized.
  397.          * (a) nTinyBits == 0: Shift left or right appropriately
  398.          *     to align the binary point at the extreme right, i.e.
  399.          *     where a long int point is expected to be. The integer
  400.          *     result is easily converted to a string.
  401.          * (b) nTinyBits > 0: Shift right by expShift-nFractBits,
  402.          *     which effectively converts to long and scales by
  403.          *     2^nTinyBits. Then multiply by 5^nTinyBits to
  404.          *     complete the scaling. We know this won't overflow
  405.          *     because we just counted the number of bits necessary
  406.          *     in the result. The integer you get from this can
  407.          *     then be converted to a string pretty easily.
  408.          */
  409.         long halfULP;
  410.         if ( nTinyBits == 0 ) {
  411.             if ( binExp > nSignificantBits ){
  412.             halfULP = 1L << ( binExp-nSignificantBits-1);
  413.             } else {
  414.             halfULP = 0L;
  415.             }
  416.             if ( binExp >= expShift ){
  417.             fractBits <<= (binExp-expShift);
  418.             } else {
  419.             fractBits >>>= (expShift-binExp) ;
  420.             }
  421.             developLongDigits( 0, fractBits, halfULP );
  422.             return;
  423.         }
  424.         /*
  425.          * The following causes excess digits to be printed
  426.          * out in the single-float case. Our manipulation of
  427.          * halfULP here is apparently not correct. If we
  428.          * better understand how this works, perhaps we can
  429.          * use this special case again. But for the time being,
  430.          * we do not.
  431.          * else {
  432.          *     fractBits >>>= expShift+1-nFractBits;
  433.          *     fractBits *= long5pow[ nTinyBits ];
  434.          *     halfULP = long5pow[ nTinyBits ] >> (1+nSignificantBits-nFractBits);
  435.          *     developLongDigits( -nTinyBits, fractBits, halfULP );
  436.          *     return;
  437.          * }
  438.          */
  439.         }
  440.     }
  441.     /*
  442.      * This is the hard case. We are going to compute large positive
  443.      * integers B and S and integer decExp, s.t.
  444.      *    d = ( B / S ) * 10^decExp
  445.      *    1 <= B / S < 10
  446.      * Obvious choices are:
  447.      *    decExp = floor( log10(d) )
  448.      *     B      = d * 2^nTinyBits * 10^max( 0, -decExp )
  449.      *    S      = 10^max( 0, decExp) * 2^nTinyBits
  450.      * (noting that nTinyBits has already been forced to non-negative)
  451.      * I am also going to compute a large positive integer
  452.      *    M      = (1/2^nSignificantBits) * 2^nTinyBits * 10^max( 0, -decExp )
  453.      * i.e. M is (1/2) of the ULP of d, scaled like B.
  454.      * When we iterate through dividing B/S and picking off the
  455.      * quotient bits, we will know when to stop when the remainder
  456.      * is <= M.
  457.      *
  458.      * We keep track of powers of 2 and powers of 5.
  459.      */
  460.  
  461.     /*
  462.      * Estimate decimal exponent. (If it is small-ish,
  463.      * we could double-check.)
  464.      *
  465.      * First, scale the mantissa bits such that 1 <= d2 < 2.
  466.      * We are then going to estimate
  467.      *        log10(d2) ~=~  (d2-1.5)/1.5 + log(1.5) 
  468.      * and so we can estimate 
  469.      *      log10(d) ~=~ log10(d2) + binExp * log10(2)
  470.      * take the floor and call it decExp.
  471.      * FIXME -- use more precise constants here. It costs no more.
  472.      */
  473.     double d2 = Double.longBitsToDouble( 
  474.         expOne | ( fractBits &~ fractHOB ) );
  475.     decExp = (int)Math.floor(
  476.         (d2-1.5D)*0.289529654D + 0.176091259 + (double)binExp * 0.301029995663981 );
  477.     int B2, B5; // powers of 2 and powers of 5, respectively, in B
  478.     int S2, S5; // powers of 2 and powers of 5, respectively, in S
  479.     int M2, M5; // powers of 2 and powers of 5, respectively, in M
  480.     int Bbits; // binary digits needed to represent B, approx.
  481.     int tenSbits; // binary digits needed to represent 10*S, approx.
  482.     FDBigInt Sval, Bval, Mval;
  483.  
  484.     B5 = Math.max( 0, -decExp );
  485.     B2 = B5 + nTinyBits + binExp;
  486.  
  487.     S5 = Math.max( 0, decExp );
  488.     S2 = S5 + nTinyBits;
  489.  
  490.     M5 = B5;
  491.     M2 = B2 - nSignificantBits;
  492.  
  493.     /*
  494.      * the long integer fractBits contains the (nFractBits) interesting
  495.      * bits from the mantissa of d ( hidden 1 added if necessary) followed
  496.      * by (expShift+1-nFractBits) zeros. In the interest of compactness,
  497.      * I will shift out those zeros before turning fractBits into a
  498.      * FDBigInt. The resulting whole number will be 
  499.      *     d * 2^(nFractBits-1-binExp).
  500.      */
  501.     fractBits >>>= (expShift+1-nFractBits);
  502.     B2 -= nFractBits-1;
  503.     int common2factor = Math.min( B2, S2 );
  504.     B2 -= common2factor;
  505.     S2 -= common2factor;
  506.     M2 -= common2factor;
  507.  
  508.     /*
  509.      * HACK!! For exact powers of two, the next smallest number
  510.      * is only half as far away as we think (because the meaning of
  511.      * ULP changes at power-of-two bounds) for this reason, we
  512.      * hack M2. Hope this works.
  513.      */
  514.     if ( nFractBits == 1 )
  515.         M2 -= 1;
  516.  
  517.     if ( M2 < 0 ){
  518.         // oops. 
  519.         // since we cannot scale M down far enough,
  520.         // we must scale the other values up.
  521.         B2 -= M2;
  522.         S2 -= M2;
  523.         M2 =  0;
  524.     }
  525.     /*
  526.      * Construct, Scale, iterate.
  527.      * Some day, we'll write a stopping test that takes
  528.      * account of the assymetry of the spacing of floating-point
  529.      * numbers below perfect powers of 2
  530.      * 26 Sept 96 is not that day.
  531.      * So we use a symmetric test.
  532.      */
  533.     char digits[] = this.digits = new char[18];
  534.     int  ndigit = 0;
  535.     boolean low, high;
  536.     long lowDigitDifference;
  537.     int  q;
  538.  
  539.     /*
  540.      * Detect the special cases where all the numbers we are about
  541.      * to compute will fit in int or long integers.
  542.      * In these cases, we will avoid doing FDBigInt arithmetic.
  543.      * We use the same algorithms, except that we "normalize"
  544.      * our FDBigInts before iterating. This is to make division easier,
  545.      * as it makes our fist guess (quotient of high-order words)
  546.      * more accurate!
  547.      *
  548.      * Some day, we'll write a stopping test that takes
  549.      * account of the assymetry of the spacing of floating-point
  550.      * numbers below perfect powers of 2
  551.      * 26 Sept 96 is not that day.
  552.      * So we use a symmetric test.
  553.      */
  554.     Bbits = nFractBits + B2 + (( B5 < n5bits.length )? n5bits[B5] : ( B5*3 ));
  555.     tenSbits = S2+1 + (( (S5+1) < n5bits.length )? n5bits[(S5+1)] : ( (S5+1)*3 ));
  556.     if ( Bbits < 64 && tenSbits < 64){
  557.         if ( Bbits < 32 && tenSbits < 32){
  558.         // wa-hoo! They're all ints!
  559.         int b = ((int)fractBits * small5pow[B5] ) << B2;
  560.         int s = small5pow[S5] << S2;
  561.         int m = small5pow[M5] << M2;
  562.         int tens = s * 10;
  563.         /*
  564.          * Unroll the first iteration. If our decExp estimate
  565.          * was too high, our first quotient will be zero. In this
  566.          * case, we discard it and decrement decExp.
  567.          */
  568.         ndigit = 0;
  569.         q = (int) ( b / s );
  570.         b = 10 * ( b % s );
  571.         m *= 10;
  572.         low  = (b <  m );
  573.         high = (b+m > tens );
  574.         if ( q >= 10 ){
  575.             // bummer, dude
  576.             throw new RuntimeException( "Assertion botch: excessivly large digit "+q);
  577.         } else if ( (q == 0) && ! high ){
  578.             // oops. Usually ignore leading zero.
  579.             decExp--;
  580.         } else {
  581.             digits[ndigit++] = (char)('0' + q);
  582.         }
  583.         /*
  584.          * HACK! Java spec sez that we always have at least
  585.          * one digit after the . in either F- or E-form output.
  586.          * Thus we will need more than one digit if we're using
  587.          * E-form
  588.          */
  589.         if ( decExp <= -3 || decExp >= 8 ){
  590.             high = low = false;
  591.         }
  592.         while( ! low && ! high ){
  593.             q = (int) ( b / s );
  594.             b = 10 * ( b % s );
  595.             m *= 10;
  596.             if ( q >= 10 ){
  597.             // bummer, dude
  598.             throw new RuntimeException( "Assertion botch: excessivly large digit "+q);
  599.             }
  600.             if ( m > 0L ){
  601.             low  = (b <  m );
  602.             high = (b+m > tens );
  603.             } else {
  604.             // hack -- m might overflow!
  605.             // in this case, it is certainly > b,
  606.             // which won't
  607.             // and b+m > tens, too, since that has overflowed
  608.             // either!
  609.             low = true;
  610.             high = true;
  611.             }
  612.             digits[ndigit++] = (char)('0' + q);
  613.         }
  614.         lowDigitDifference = (b<<1) - tens;
  615.         } else {
  616.         // still good! they're all longs!
  617.         long b = (fractBits * long5pow[B5] ) << B2;
  618.         long s = long5pow[S5] << S2;
  619.         long m = long5pow[M5] << M2;
  620.         long tens = s * 10L;
  621.         /*
  622.          * Unroll the first iteration. If our decExp estimate
  623.          * was too high, our first quotient will be zero. In this
  624.          * case, we discard it and decrement decExp.
  625.          */
  626.         ndigit = 0;
  627.         q = (int) ( b / s );
  628.         b = 10L * ( b % s );
  629.         m *= 10L;
  630.         low  = (b <  m );
  631.         high = (b+m > tens );
  632.         if ( q >= 10 ){
  633.             // bummer, dude
  634.             throw new RuntimeException( "Assertion botch: excessivly large digit "+q);
  635.         } else if ( (q == 0) && ! high ){
  636.             // oops. Usually ignore leading zero.
  637.             decExp--;
  638.         } else {
  639.             digits[ndigit++] = (char)('0' + q);
  640.         }
  641.         /*
  642.          * HACK! Java spec sez that we always have at least
  643.          * one digit after the . in either F- or E-form output.
  644.          * Thus we will need more than one digit if we're using
  645.          * E-form
  646.          */
  647.         if ( decExp <= -3 || decExp >= 8 ){
  648.             high = low = false;
  649.         }
  650.         while( ! low && ! high ){
  651.             q = (int) ( b / s );
  652.             b = 10 * ( b % s );
  653.             m *= 10;
  654.             if ( q >= 10 ){
  655.             // bummer, dude
  656.             throw new RuntimeException( "Assertion botch: excessivly large digit "+q);
  657.             }
  658.             if ( m > 0L ){
  659.             low  = (b <  m );
  660.             high = (b+m > tens );
  661.             } else {
  662.             // hack -- m might overflow!
  663.             // in this case, it is certainly > b,
  664.             // which won't
  665.             // and b+m > tens, too, since that has overflowed
  666.             // either!
  667.             low = true;
  668.             high = true;
  669.             }
  670.             digits[ndigit++] = (char)('0' + q);
  671.         }
  672.         lowDigitDifference = (b<<1) - tens;
  673.         }
  674.     } else {
  675.         FDBigInt tenSval;
  676.         int  shiftBias;
  677.  
  678.         /*
  679.          * We really must do FDBigInt arithmetic.
  680.          * Fist, construct our FDBigInt initial values.
  681.          */
  682.         Bval = new FDBigInt( fractBits  );
  683.         if ( B5 != 0 ){
  684.         if ( B5 < small5pow.length ){
  685.             Bval = Bval.mult( small5pow[B5] );
  686.         } else {
  687.             Bval = Bval.mult( big5pow( B5 ) );
  688.         }
  689.         }
  690.         if ( B2 != 0 ){
  691.         Bval.lshiftMe( B2 );
  692.         }
  693.         Sval = new FDBigInt( big5pow( S5 ) );
  694.         if ( S2 != 0 ){
  695.         Sval.lshiftMe( S2 );
  696.         }
  697.         Mval = new FDBigInt( big5pow( M5 ) );
  698.         if ( M2 != 0 ){
  699.         Mval.lshiftMe( M2 );
  700.         }
  701.  
  702.  
  703.         // normalize so that division works better
  704.         Bval.lshiftMe( shiftBias = Sval.normalizeMe() );
  705.         Mval.lshiftMe( shiftBias );
  706.         tenSval = Sval.mult( 10 );
  707.         /*
  708.          * Unroll the first iteration. If our decExp estimate
  709.          * was too high, our first quotient will be zero. In this
  710.          * case, we discard it and decrement decExp.
  711.          */
  712.         ndigit = 0;
  713.         q = Bval.quoRemIteration( Sval );
  714.         Mval = Mval.mult( 10 );
  715.         low  = (Bval.cmp( Mval ) < 0);
  716.         high = (Bval.add( Mval ).cmp( tenSval ) > 0 );
  717.         if ( q >= 10 ){
  718.         // bummer, dude
  719.         throw new RuntimeException( "Assertion botch: excessivly large digit "+q);
  720.         } else if ( (q == 0) && ! high ){
  721.         // oops. Usually ignore leading zero.
  722.         decExp--;
  723.         } else {
  724.         digits[ndigit++] = (char)('0' + q);
  725.         }
  726.         /*
  727.          * HACK! Java spec sez that we always have at least
  728.          * one digit after the . in either F- or E-form output.
  729.          * Thus we will need more than one digit if we're using
  730.          * E-form
  731.          */
  732.         if ( decExp <= -3 || decExp >= 8 ){
  733.         high = low = false;
  734.         }
  735.         while( ! low && ! high ){
  736.         q = Bval.quoRemIteration( Sval );
  737.         Mval = Mval.mult( 10 );
  738.         if ( q >= 10 ){
  739.             // bummer, dude
  740.             throw new RuntimeException( "Assertion botch: excessivly large digit "+q);
  741.         }
  742.         low  = (Bval.cmp( Mval ) < 0);
  743.         high = (Bval.add( Mval ).cmp( tenSval ) > 0 );
  744.         digits[ndigit++] = (char)('0' + q);
  745.         }
  746.         if ( high && low ){
  747.         Bval.lshiftMe(1);
  748.         lowDigitDifference = Bval.cmp(tenSval);
  749.         } else
  750.         lowDigitDifference = 0L; // this here only for flow analysis!
  751.     }
  752.     this.decExponent = decExp+1;
  753.     this.digits = digits;
  754.     this.nDigits = ndigit;
  755.     /*
  756.      * Last digit gets rounded based on stopping condition.
  757.      */
  758.     if ( high ){
  759.         if ( low ){
  760.         if ( lowDigitDifference == 0L ){
  761.             // it's a tie!
  762.             // choose based on which digits we like.
  763.             if ( (digits[nDigits-1]&1) != 0 ) roundup();
  764.         } else if ( lowDigitDifference > 0 ){
  765.             roundup();
  766.         }
  767.         } else {
  768.         roundup();
  769.         }
  770.     }
  771.     }
  772.  
  773.     public String
  774.     toString(){
  775.     // most brain-dead version
  776.     StringBuffer result = new StringBuffer( nDigits+8 );
  777.     if ( isNegative ){ result.append( '-' ); }
  778.     if ( isExceptional ){
  779.         result.append( digits, 0, nDigits );
  780.     } else {
  781.         result.append( "0.");
  782.         result.append( digits, 0, nDigits );
  783.         result.append('e');
  784.         result.append( decExponent );
  785.     }
  786.     return new String(result);
  787.     }
  788.  
  789.     public String
  790.     toJavaFormatString(){
  791.     char result[] = new char[ nDigits + 10 ];
  792.     int  i = 0;
  793.     if ( isNegative ){ result[0] = '-'; i = 1; }
  794.     if ( isExceptional ){
  795.         System.arraycopy( digits, 0, result, i, nDigits );
  796.         i += nDigits;
  797.     } else {
  798.         if ( decExponent > 0 && decExponent < 8 ){
  799.         // print digits.digits.
  800.         int charLength = Math.min( nDigits, decExponent );
  801.         System.arraycopy( digits, 0, result, i, charLength );
  802.         i += charLength;
  803.         if ( charLength < decExponent ){
  804.             charLength = decExponent-charLength;
  805.             System.arraycopy( zero, 0, result, i, charLength );
  806.             i += charLength;
  807.             result[i++] = '.';
  808.             result[i++] = '0';
  809.         } else {
  810.             result[i++] = '.';
  811.             if ( charLength < nDigits ){
  812.             int t = nDigits - charLength;
  813.             System.arraycopy( digits, charLength, result, i, t );
  814.             i += t;
  815.             } else{
  816.             result[i++] = '0';
  817.             }
  818.         }
  819.         } else if ( decExponent <=0 && decExponent > -3 ){
  820.         result[i++] = '0';
  821.         result[i++] = '.';
  822.         if ( decExponent != 0 ){
  823.             System.arraycopy( zero, 0, result, i, -decExponent );
  824.             i -= decExponent;
  825.         }
  826.         System.arraycopy( digits, 0, result, i, nDigits );
  827.         i += nDigits;
  828.         } else {
  829.         result[i++] = digits[0];
  830.         result[i++] = '.';
  831.         if ( nDigits > 1 ){
  832.             System.arraycopy( digits, 1, result, i, nDigits-1 );
  833.             i += nDigits-1;
  834.         } else {
  835.             result[i++] = '0';
  836.         }
  837.         result[i++] = 'E';
  838.         int e;
  839.         if ( decExponent <= 0 ){
  840.             result[i++] = '-';
  841.             e = -decExponent+1;
  842.         } else {
  843.             e = decExponent-1;
  844.         }
  845.         // decExponent has 1, 2, or 3, digits
  846.         if ( e <= 9 ) {
  847.             result[i++] = (char)( e+'0' );
  848.         } else if ( e <= 99 ){
  849.             result[i++] = (char)( e/10 +'0' );
  850.             result[i++] = (char)( e%10 + '0' );
  851.         } else {
  852.             result[i++] = (char)(e/100+'0');
  853.             e %= 100;
  854.             result[i++] = (char)(e/10+'0');
  855.             result[i++] = (char)( e%10 + '0' );
  856.         }
  857.         }
  858.     }
  859.     return new String(result, 0, i);
  860.     }
  861.  
  862.     private static final int small5pow[] = {
  863.     1,
  864.     5,
  865.     5*5,
  866.     5*5*5,
  867.     5*5*5*5,
  868.     5*5*5*5*5,
  869.     5*5*5*5*5*5,
  870.     5*5*5*5*5*5*5,
  871.     5*5*5*5*5*5*5*5,
  872.     5*5*5*5*5*5*5*5*5,
  873.     5*5*5*5*5*5*5*5*5*5,
  874.     5*5*5*5*5*5*5*5*5*5*5,
  875.     5*5*5*5*5*5*5*5*5*5*5*5,
  876.     5*5*5*5*5*5*5*5*5*5*5*5*5
  877.     };
  878.  
  879.     private static final long long5pow[] = {
  880.     1L,
  881.     5L,
  882.     5L*5,
  883.     5L*5*5,
  884.     5L*5*5*5,
  885.     5L*5*5*5*5,
  886.     5L*5*5*5*5*5,
  887.     5L*5*5*5*5*5*5,
  888.     5L*5*5*5*5*5*5*5,
  889.     5L*5*5*5*5*5*5*5*5,
  890.     5L*5*5*5*5*5*5*5*5*5,
  891.     5L*5*5*5*5*5*5*5*5*5*5,
  892.     5L*5*5*5*5*5*5*5*5*5*5*5,
  893.     5L*5*5*5*5*5*5*5*5*5*5*5*5,
  894.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5,
  895.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  896.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  897.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  898.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  899.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  900.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  901.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  902.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  903.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  904.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  905.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  906.     5L*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5*5,
  907.     };
  908.  
  909.     // approximately ceil( log2( long5pow[i] ) )
  910.     private static final int n5bits[] = {
  911.     0,
  912.     3,
  913.     5,
  914.     7,
  915.     10,
  916.     12,
  917.     14,
  918.     17,
  919.     19,
  920.     21,
  921.     24,
  922.     26,
  923.     28,
  924.     31,
  925.     33,
  926.     35,
  927.     38,
  928.     40,
  929.     42,
  930.     45,
  931.     47,
  932.     49,
  933.     52,
  934.     54,
  935.     56,
  936.     59,
  937.     61,
  938.     };
  939.  
  940.     private static final char infinity[] = { 'I', 'n', 'f', 'i', 'n', 'i', 't', 'y' };
  941.     private static final char notANumber[] = { 'N', 'a', 'N' };
  942.     private static final char zero[] = { '0', '0', '0', '0', '0', '0', '0', '0' };
  943. }
  944.  
  945. /*
  946.  * A really, really simple bigint package
  947.  * tailored to the needs of floating base conversion.
  948.  */
  949. class FDBigInt {
  950.     int    nWords; // number of words used
  951.     int data[]; // value: data[0] is least significant
  952.  
  953.     private static boolean debugging = false;
  954.  
  955.     public static void setDebugging( boolean d ) { debugging = d; }
  956.  
  957.     public FDBigInt( int v ){
  958.     nWords = 1;
  959.     data = new int[1];
  960.     data[0] = v;
  961.     }
  962.  
  963.     public FDBigInt( long v ){
  964.     data = new int[2];
  965.     data[0] = (int)v;
  966.     data[1] = (int)(v>>>32);
  967.     nWords = (data[1]==0) ? 1 : 2;
  968.     }
  969.  
  970.     public FDBigInt( FDBigInt other ){
  971.     data = new int[nWords = other.nWords];
  972.     System.arraycopy( other.data, 0, data, 0, nWords );
  973.     }
  974.  
  975.     private FDBigInt( int [] d, int n ){
  976.     data = d;
  977.     nWords = n;
  978.     }
  979.  
  980.     /*
  981.      * Left shift by c bits.
  982.      * Shifts this in place.
  983.      */
  984.     public void
  985.     lshiftMe( int c )throws IllegalArgumentException {
  986.     if ( c <= 0 ){
  987.         if ( c == 0 )
  988.         return; // silly.
  989.         else
  990.         throw new IllegalArgumentException("negative shift count");
  991.     }
  992.     int wordcount = c>>5;
  993.     int bitcount  = c & 0x1f;
  994.     int anticount = 32-bitcount;
  995.     int t[] = data;
  996.     int s[] = data;
  997.     if ( nWords+wordcount+1 > t.length ){
  998.         // reallocate.
  999.         t = new int[ nWords+wordcount+1 ];
  1000.     }
  1001.     int target = nWords+wordcount;
  1002.     int src    = nWords-1;
  1003.     if ( bitcount == 0 ){
  1004.         // special hack, since an anticount of 32 won't go!
  1005.         System.arraycopy( s, 0, t, wordcount, nWords );
  1006.         target = wordcount-1;
  1007.     } else {
  1008.         t[target--] = s[src]>>>anticount;
  1009.         while ( src >= 1 ){
  1010.         t[target--] = (s[src]<<bitcount) | (s[--src]>>>anticount);
  1011.         }
  1012.         t[target--] = s[src]<<bitcount;
  1013.     }
  1014.     while( target >= 0 ){
  1015.         t[target--] = 0;
  1016.     }
  1017.     data = t;
  1018.     nWords += wordcount + 1;
  1019.     // may have constructed high-order word of 0.
  1020.     // if so, trim it
  1021.     while ( nWords > 1 && data[nWords-1] == 0 )
  1022.         nWords--;
  1023.     }
  1024.  
  1025.     /*
  1026.      * normalize this number by shifting until
  1027.      * the MSB of the number is at 0x08000000.
  1028.      * This is in preparation for quoRemIteration, below.
  1029.      * The idea is that, to make division easier, we want the
  1030.      * divisor to be "normalized" -- usually this means shifting
  1031.      * the MSB into the high words sign bit. But because we know that
  1032.      * the quotient will be 0 < q < 10, we would like to arrange that
  1033.      * the dividend not span up into another word of precision.
  1034.      * (This needs to be explained more clearly!)
  1035.      */
  1036.     public int
  1037.     normalizeMe() throws IllegalArgumentException {
  1038.     int src;
  1039.     int wordcount = 0;
  1040.     int bitcount  = 0;
  1041.     int v = 0;
  1042.     for ( src= nWords-1 ; src >= 0 && (v=data[src]) == 0 ; src--){
  1043.         wordcount += 1;
  1044.     }
  1045.     if ( src < 0 ){
  1046.         // oops. Value is zero. Cannot normalize it!
  1047.         throw new IllegalArgumentException("zero value");
  1048.     }
  1049.     /*
  1050.      * In most cases, we assume that wordcount is zero. This only
  1051.      * makes sense, as we try not to maintain any high-order
  1052.      * words full of zeros. In fact, if there are zeros, we will
  1053.      * simply SHORTEN our number at this point. Watch closely...
  1054.      */
  1055.     nWords -= wordcount;
  1056.     /*
  1057.      * Compute how far left we have to shift v s.t. its highest-
  1058.      * order bit is in the right place. Then call lshiftMe to
  1059.      * do the work.
  1060.      */
  1061.     if ( (v & 0xf0000000) != 0 ){
  1062.         // will have to shift up into the next word.
  1063.         // too bad.
  1064.         for( bitcount = 32 ; (v & 0xf0000000) != 0 ; bitcount-- )
  1065.         v >>>= 1;
  1066.     } else {
  1067.         while ( v <= 0x000fffff ){
  1068.         // hack: byte-at-a-time shifting
  1069.         v <<= 8;
  1070.         bitcount += 8;
  1071.         }
  1072.         while ( v <= 0x07ffffff ){
  1073.         v <<= 1;
  1074.         bitcount += 1;
  1075.         }
  1076.     }
  1077.     if ( bitcount != 0 )
  1078.         lshiftMe( bitcount );
  1079.     return bitcount;
  1080.     }
  1081.  
  1082.     /*
  1083.      * Multiply a FDBigInt by an int.
  1084.      * Result is a new FDBigInt.
  1085.      */
  1086.     public FDBigInt
  1087.     mult( int iv ) {
  1088.     long v = iv;
  1089.     int r[];
  1090.     long p;
  1091.  
  1092.     // guess adequate size of r.
  1093.     r = new int[ ( v * ((long)data[nWords-1]&0xffffffffL) > 0xfffffffL ) ? nWords+1 : nWords ];
  1094.     p = 0L;
  1095.     for( int i=0; i < nWords; i++ ) {
  1096.         p += v * ((long)data[i]&0xffffffffL);
  1097.         r[i] = (int)p;
  1098.         p >>>= 32;
  1099.     }
  1100.     if ( p == 0L){
  1101.         return new FDBigInt( r, nWords );
  1102.     } else {
  1103.         r[nWords] = (int)p;
  1104.         return new FDBigInt( r, nWords+1 );
  1105.     }
  1106.     }
  1107.  
  1108.     /*
  1109.      * Multiply a FDBigInt by another FDBigInt.
  1110.      * Result is a new FDBigInt.
  1111.      */
  1112.     public FDBigInt
  1113.     mult( FDBigInt other ){
  1114.     // crudely guess adequate size for r
  1115.     int r[] = new int[ nWords + other.nWords ];
  1116.     int i;
  1117.     // I think I am promised zeros...
  1118.  
  1119.     for( i = 0; i < this.nWords; i++ ){
  1120.         long v = (long)this.data[i] & 0xffffffffL; // UNSIGNED CONVERSION
  1121.         long p = 0L;
  1122.         int j;
  1123.         for( j = 0; j < other.nWords; j++ ){
  1124.         p += ((long)r[i+j]&0xffffffffL) + v*((long)other.data[j]&0xffffffffL); // UNSIGNED CONVERSIONS ALL 'ROUND.
  1125.         r[i+j] = (int)p;
  1126.         p >>>= 32;
  1127.         }
  1128.         r[i+j] = (int)p;
  1129.     }
  1130.     // compute how much of r we actually needed for all that.
  1131.     for ( i = r.length-1; i> 0; i--)
  1132.         if ( r[i] != 0 )
  1133.         break;
  1134.     return new FDBigInt( r, i+1 );
  1135.     }
  1136.  
  1137.     /*
  1138.      * Add one FDBigInt to another. Return a FDBigInt
  1139.      */
  1140.     public FDBigInt
  1141.     add( FDBigInt other ){
  1142.     int i;
  1143.     int a[], b[];
  1144.     int n, m;
  1145.     long c = 0L;
  1146.     // arrange such that a.nWords >= b.nWords;
  1147.     // n = a.nWords, m = b.nWords
  1148.     if ( this.nWords >= other.nWords ){
  1149.         a = this.data;
  1150.         n = this.nWords;
  1151.         b = other.data;
  1152.         m = other.nWords;
  1153.     } else {
  1154.         a = other.data;
  1155.         n = other.nWords;
  1156.         b = this.data;
  1157.         m = this.nWords;
  1158.     }
  1159.     int r[] = new int[ n ];
  1160.     for ( i = 0; i < n; i++ ){
  1161.         c += (long)a[i] & 0xffffffffL;
  1162.         if ( i < m ){
  1163.         c += (long)b[i] & 0xffffffffL;
  1164.         }
  1165.         r[i] = (int) c;
  1166.         c >>= 32; // signed shift.
  1167.     }
  1168.     if ( c != 0L ){
  1169.         // oops -- carry out -- need longer result.
  1170.         int s[] = new int[ r.length+1 ];
  1171.         System.arraycopy( r, 0, s, 0, r.length );
  1172.         s[i++] = (int)c;
  1173.         return new FDBigInt( s, i );
  1174.     }
  1175.     return new FDBigInt( r, i );
  1176.     }
  1177.  
  1178.     /*
  1179.      * Subtract one FDBigInt from another. Return a FDBigInt
  1180.      * Assert that the result is positive.
  1181.      */
  1182.     public FDBigInt
  1183.     sub( FDBigInt other ){
  1184.     int r[] = new int[ this.nWords ];
  1185.     int i;
  1186.     int n = this.nWords;
  1187.     int m = other.nWords;
  1188.     int nzeros = 0;
  1189.     long c = 0L;
  1190.     for ( i = 0; i < n; i++ ){
  1191.         c += (long)this.data[i] & 0xffffffffL;
  1192.         if ( i < m ){
  1193.         c -= (long)other.data[i] & 0xffffffffL;
  1194.         }
  1195.         if ( ( r[i] = (int) c ) == 0 )
  1196.         nzeros++;
  1197.         else
  1198.         nzeros = 0;
  1199.         c >>= 32; // signed shift.
  1200.     }
  1201.     if ( c != 0L )
  1202.         throw new RuntimeException("Assertion botch: borrow out of subtract");
  1203.     while ( i < m )
  1204.         if ( other.data[i++] != 0 )
  1205.         throw new RuntimeException("Assertion botch: negative result of subtract");
  1206.     return new FDBigInt( r, n-nzeros );
  1207.     }
  1208.  
  1209.     /*
  1210.      * Compare FDBigInt with another FDBigInt. Return an integer
  1211.      * >0: this > other
  1212.      *  0: this == other
  1213.      * <0: this < other
  1214.      */
  1215.     public int
  1216.     cmp( FDBigInt other ){
  1217.         int i;
  1218.     if ( this.nWords > other.nWords ){
  1219.         // if any of my high-order words is non-zero,
  1220.         // then the answer is evident
  1221.         int j = other.nWords-1;
  1222.         for ( i = this.nWords-1; i > j ; i-- )
  1223.         if ( this.data[i] != 0 ) return 1;
  1224.     }else if ( this.nWords < other.nWords ){
  1225.         // if any of other's high-order words is non-zero,
  1226.         // then the answer is evident
  1227.         int j = this.nWords-1;
  1228.         for ( i = other.nWords-1; i > j ; i-- )
  1229.         if ( other.data[i] != 0 ) return -1;
  1230.     } else{
  1231.         i = this.nWords-1;
  1232.     }
  1233.     for ( ; i > 0 ; i-- )
  1234.         if ( this.data[i] != other.data[i] )
  1235.         break;
  1236.     // careful! want unsigned compare!
  1237.     // use brute force here.
  1238.     int a = this.data[i];
  1239.     int b = other.data[i];
  1240.     if ( a < 0 ){
  1241.         // a is really big, unsigned
  1242.         if ( b < 0 ){
  1243.         return a-b; // both big, negative
  1244.         } else {
  1245.         return 1; // b not big, answer is obvious;
  1246.         }
  1247.     } else {
  1248.         // a is not really big
  1249.         if ( b < 0 ) {
  1250.         // but b is really big
  1251.         return -1;
  1252.         } else {
  1253.         return a - b;
  1254.         }
  1255.     }
  1256.     }
  1257.  
  1258.     /*
  1259.      * Compute
  1260.      * q = (int)( this / S )
  1261.      * this = 10 * ( this mod S )
  1262.      * Return q.
  1263.      * This is the iteration step of digit development for output.
  1264.      * We assume that S has been normalized, as above, and that
  1265.      * "this" has been lshift'ed accordingly.
  1266.      * Also assume, of course, that the result, q, can be expressed
  1267.      * as an integer, 0 <= q < 10.
  1268.      */
  1269.     public int
  1270.     quoRemIteration( FDBigInt S )throws IllegalArgumentException {
  1271.     // ensure that this and S have the same number of
  1272.     // digits. If S is properly normalized and q < 10 then
  1273.     // this must be so.
  1274.     if ( nWords != S.nWords ){
  1275.         throw new IllegalArgumentException("disparate values");
  1276.     }
  1277.     // estimate q the obvious way. We will usually be
  1278.     // right. If not, then we're only off by a little and
  1279.     // will re-add.
  1280.     int n = nWords-1;
  1281.     long q = ((long)data[n]&0xffffffffL) / (long)S.data[n];
  1282.     long diff = 0L;
  1283.     for ( int i = 0; i <= n ; i++ ){
  1284.         diff += ((long)data[i]&0xffffffffL) -  q*((long)S.data[i]&0xffffffffL);
  1285.         data[i] = (int)diff;
  1286.         diff >>= 32; // N.B. SIGNED shift.
  1287.     }
  1288.     if ( diff != 0L ) {
  1289.         // damn, damn, damn. q is too big.
  1290.         // add S back in until this turns +. This should
  1291.         // not be very many times!
  1292.         long sum = 0L;
  1293.         while ( sum ==  0L ){
  1294.         sum = 0L;
  1295.         for ( int i = 0; i <= n; i++ ){
  1296.             sum += ((long)data[i]&0xffffffffL) +  ((long)S.data[i]&0xffffffffL);
  1297.             data[i] = (int) sum;
  1298.             sum >>= 32; // Signed or unsigned, answer is 0 or 1
  1299.         }
  1300.         /*
  1301.          * Originally the following line read
  1302.          * "if ( sum !=0 && sum != -1 )"
  1303.          * but that would be wrong, because of the 
  1304.          * treatment of the two values as entirely unsigned,
  1305.          * it would be impossible for a carry-out to be interpreted
  1306.          * as -1 -- it would have to be a single-bit carry-out, or
  1307.          * +1.
  1308.          */
  1309.         if ( sum !=0 && sum != 1 )
  1310.             throw new RuntimeException("Assertion botch: "+sum+" carry out of division correction");
  1311.         q -= 1;
  1312.         }
  1313.     }
  1314.     // finally, we can multiply this by 10.
  1315.     // it cannot overflow, right, as the high-order word has
  1316.     // at least 4 high-order zeros!
  1317.     long p = 0L;
  1318.     for ( int i = 0; i <= n; i++ ){
  1319.         p += 10*((long)data[i]&0xffffffffL);
  1320.         data[i] = (int)p;
  1321.         p >>= 32; // SIGNED shift.
  1322.     }
  1323.     if ( p != 0L )
  1324.         throw new RuntimeException("Assertion botch: carry out of *10");
  1325.  
  1326.     return (int)q;
  1327.     }
  1328.  
  1329.     public long
  1330.     longValue(){
  1331.     // if this can be represented as a long,
  1332.     // return the value
  1333.     int i;
  1334.     for ( i = this.nWords-1; i > 1 ; i-- ){
  1335.         if ( data[i] != 0 ){
  1336.         throw new RuntimeException("Assertion botch: value too big");
  1337.         }
  1338.     }
  1339.     switch(i){
  1340.     case 1:
  1341.         if ( data[1] < 0 )
  1342.         throw new RuntimeException("Assertion botch: value too big");
  1343.         return ((long)(data[1]) << 32) | ((long)data[0]&0xffffffffL);
  1344.     case 0:
  1345.         return ((long)data[0]&0xffffffffL);
  1346.     default:
  1347.         throw new RuntimeException("Assertion botch: longValue confused");
  1348.     }
  1349.     }
  1350.  
  1351.     public String
  1352.     toString() {
  1353.     StringBuffer r = new StringBuffer(30);
  1354.     r.append('[');
  1355.     int i = Math.min( nWords-1, data.length-1) ;
  1356.     if ( nWords > data.length ){
  1357.         r.append( "("+data.length+"<"+nWords+"!)" );
  1358.     }
  1359.     for( ; i> 0 ; i-- ){
  1360.         r.append( Integer.toHexString( data[i] ) );
  1361.         r.append( (char) ' ' );
  1362.     }
  1363.     r.append( Integer.toHexString( data[0] ) );
  1364.     r.append( (char) ']' );
  1365.     return new String( r );
  1366.     }
  1367. }
  1368.